package com.codingfuture.erp_system.web.controller;

import com.codingfuture.erp_system.dto.EmpDto;
import com.codingfuture.erp_system.dto.PageData;
import com.codingfuture.erp_system.entity.Dep;
import com.codingfuture.erp_system.entity.Emp;
import com.codingfuture.erp_system.service.EmpService;
import com.codingfuture.erp_system.util.CheckUtils;
import com.codingfuture.erp_system.util.Result;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@Slf4j
@RequestMapping("/v2")
@RestController
public class EmpController {
    @Autowired
    private EmpService empService;
    private final String em = "";

    //    id查找
    @GetMapping("/emp/selectone")
    public Result findById(Integer id, HttpSession session) {
        session.setAttribute("empId", id);
        Emp emp = empService.findById(id);
        return Result.OK(emp);
    }

    //新增
    @PostMapping("/emp/add")
    public Result insert(Emp emp) {
        Integer dep = emp.getDep();
        String username = emp.getUsername();
        if (username == null || username.equals(em)) {
            return Result.error(400, "请正确输入");
        }
        //校验email格式
        else if (CheckUtils.checkEmail(emp.getEmail())) {
            return Result.error(400, "邮箱格式不正确");
        }
        //校验联系电话
        else if (CheckUtils.checkMobile(emp.getTele())) {
            return Result.error(400, "联系电话格式正确，需要11位数字电话号码");
        }
        //校验名称合法性
        else if (CheckUtils.checkChinese(emp.getName())) {
            return Result.error(400, "姓名错误");
        }
        //校验联系地址合法性
        else if (CheckUtils.checkChinese(emp.getAddress())) {
            return Result.error(400, "联系地址不是中文");
        }
        // 部门不能为空
        else if (dep == null) {
            return Result.error(400, "部门不能为空");
        } else if (empService.userNameExist(username) == null) {
            boolean result = empService.insert(emp);
            return result ? Result.ok() : Result.error(500, "添加失败");
        }
        return Result.error(400, "输入内容重复");

    }

    //    编辑
    @PostMapping("emp/update")
    public Result<?> update(Emp emp, HttpSession session) {
        Object id = session.getAttribute("empId");
        emp.setUuid((Integer) id);
        Integer dep = emp.getDep();
        emp.setDepuuid(dep);
        String username = emp.getUsername();
        if (username == null || username.equals(em)) {
            return Result.error(400, "请正确输入");
        } //校验email格式
        else if (CheckUtils.checkEmail(emp.getEmail())) {
            return Result.error(400, "邮箱格式不正确");
        }
        //校验联系电话
        else if (CheckUtils.checkMobile(emp.getTele())) {
            return Result.error(400, "联系电话格式不正确，需要11位数字电话号码");
        }
        //校验名称合法性
        else if (CheckUtils.checkChinese(emp.getName())) {
            return Result.error(400, "姓名错误");
        }
        //校验联系地址合法性
        else if (CheckUtils.checkChinese(emp.getAddress())) {
            return Result.error(400, "联系地址不是中文");
        } else if (dep == null) {
            return Result.error(400, "部门不能为空");
        } else if (empService.userNameExist(username) == null) {
            boolean result = empService.update(emp);
            return result ? Result.ok() : Result.error(500, "失败");
        }
        return Result.error(400, "输入内容重复");
    }

    //删除
    @DeleteMapping("/emp")
    public Result delete(Integer id) {
        empService.delete(id);
        return Result.ok();
    }

    //   表格渲染
    @GetMapping("/emp/listByPage")
    public Result<?> findByAll(@RequestParam(defaultValue = "1") int page,
                               @RequestParam(defaultValue = "10") int rows,
                               EmpDto empDto) {
        Page<Object> page1 = PageHelper.startPage(page, rows);
        List<EmpDto> list = empService.findByAll(empDto);
        PageData<EmpDto> data = new PageData<>(list, page1.getTotal());
        return Result.ok(data);
    }

    //获取
    @GetMapping("/emp/deplist")
    public Result findAll() {
        List<Dep> list = empService.finAll();
        return Result.OK(list);
    }


}
